<%@ page import="java.util.List" %>
<%--
  Created by IntelliJ IDEA.
  User: Gao
  Date: 2023/3/16
  Time: 16:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>Title</title>
    <%@ include file="header.jsp"%>
</head>
<body>
    <div class="demoTable">
        名字：
        <div class="layui-inline">
            <input class="layui-input" name="name" id="nameId" autocomplete="off">
        </div>
        年龄：
        <div class="layui-inline">
            <input class="layui-input" name="age" id="ageId" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="imageTemplet">
        <image src="/pic/{{d.image}}"></image>
    </script>

    <script type="text/html" id="statusTemplet">
        {{#      if(d.status == 1) {                }}
                    上架
        {{#      } else if(d.status == 2) {         }}
                    下架
        {{#      }                                  }}
    </script>

    <script>
        /*layui.use(['table'], function() {

        });*/
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,url:'${pageContext.request.contextPath}/blog?method=selectByPage'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '用户数据表'
                ,cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID',  fixed: 'left', sort: true}
                    ,{field:'title', title:'标题'}
                    ,{field:'image', title:'图片', templet:'#imageTemplet'}
                    ,{field:'clickCount', title:'点击数量'}
                    ,{field:'content', title:'博客内容'}
                    ,{field:'typeId', title:'分类'}
                    ,{field:'status', title:'状态', templet:'#statusTemplet'}
                    ,{field:'gmtCreate', title:'创建时间'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]]
                ,page: true
                ,id: 'tableId'
            });

            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['950px', '600px'],
                            content: '${pageContext.request.contextPath}/blog_add.jsp'
                        });
                        break;
                    case 'deleteAll':
                        //[{"id":4,"name":"4","password":null,"age":0,"address":null},{"id":6,"name":"6","password":null,"age":0,"address":null}]
                        var data = checkStatus.data;
                        var ids = [];
                        $(data).each(function() {
                            ids.push(this.id);
                        });
                        //[4,6]
                        //ids = ids.join(',');// '4,6'
                        layer.confirm('真的删除行么', function(index){
                            $.post(
                                '${pageContext.request.contextPath}/blog?method=deleteAll',
                                {'ids': ids},
                                function(jsonResult) {
                                    if (jsonResult.ok) {
                                        mylayer.okMsg(jsonResult.msg);
                                        //删除之后重新刷新table表格
                                        table.reload('tableId');
                                    } else {
                                        mylayer.errorMsg(jsonResult.msg)
                                    }
                                },
                                'json'
                            );
                            layer.close(index);
                        });
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                console.log(obj)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        $.post(
                            '${pageContext.request.contextPath}/blog?method=deleteById',
                            {'id': data.id},
                            function(jsonResult) {
                                console.log(jsonResult);
                                //JS对象：{code: 0, msg: '删除成功', data: null, ok: true}
                                //Network中返回JSON: {"code":0,"msg":"删除成功","data":null,"ok":true}
                                if (jsonResult.ok) {
                                    mylayer.okMsg(jsonResult.msg);
                                    //删除之后重新刷新table表格
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(jsonResult.msg)
                                }
                            },
                            'json'
                        );
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    layer.open({
                        type: 2,
                        area: ['550px', '350px'],
                        content: '${pageContext.request.contextPath}/blog?method=getBlogUpdatePage&id=' + data.id
                    });
                }
            });


            var $ = layui.$, active = {
                reload: function(){
                    //执行重载
                    table.reload('tableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            //JavaWeb/blog?method=selectByPage&page=1&limit=10&name=aa&age=23
                           name: $('#nameId').val(),
                           age: $('#ageId').val(),
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
</body>
</html>
